#include "task.h"

#ifndef NULL
#define NULL 0
#endif

int SubTask::getType() const
{
    return type;
}

void SuperSubTaskHandlerMgr::addSuperSubTaskHandler(int aSubTaskType, SuperSubTaskHandler* pHandler)
{
    if (MAX_SUBTASK_TYPE_NUM < aSubTaskType && aSubTaskType >= 0)
    {
        m_superSubTaskHandlers[aSubTaskType] = pHandler;
    }
}

bool SuperSubTaskHandlerMgr::isComplete(const SubTask& aASubTask)
{
    int    type = aASubTask.getType();
    if (type < MAX_SUBTASK_TYPE_NUM && type >= 0)
    {
        SuperSubTaskHandler* pHandler = m_superSubTaskHandlers[type];
        if (NULL != pHandler)
        {
            return pHandler->isComplete(aASubTask);
        }
    }

    return true;
}

void SuperSubTaskHandlerMgr::updateData(SubTask& aASubTask, int aEventID, void* arg)
{
    int    type = aASubTask.getType();
    if (type < MAX_SUBTASK_TYPE_NUM && type >= 0)
    {
        SuperSubTaskHandler* pHandler = m_superSubTaskHandlers[type];
        if (NULL != pHandler)
        {
            return pHandler->updateData(aASubTask, aEventID, arg);
        }
    }
}

int Task::getType() const
{
    return type;
}

bool Task::isComplete()    const
{
    for (int i = 0; i < MAX_SUB_TASK_NUM; ++i)
    {
        if (!g_superSubTaskHandlerMgr.isComplete(m_subTasks[i]))
        {
            return false;
        }
    }

    return true;
}

void Task::updateData(int aEventID, void* arg)
{
    for (int i = 0; i < MAX_SUB_TASK_NUM; ++i)
    {
        g_superSubTaskHandlerMgr.updateData(m_subTasks[i], aEventID, arg);
    }
}

void SuperTaskHandlerMgr::addSuperTaskHandler(int aTaskType, SuperTaskHandler* pHandler)
{
    if (MAX_TASKTYPE_NUM < aTaskType && aTaskType >= 0)
    {
        m_superTaskHandlers[aTaskType] = pHandler;
    }
}

SuperSubTaskHandlerMgr g_superSubTaskHandlerMgr;
SuperTaskHandlerMgr g_superTaskHandlerMgr;
